package edu.uic.cs.i422.s2011.team4;

import java.util.ArrayList;

import edu.uic.cs.i422.s2011.team4.SquirrelData.FeedType;
import edu.uic.cs.i422.s2011.team4.SquirrelData.SiteType;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

public class FeedingObservation extends Activity implements OnClickListener {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		  
		      View inflatedView = View.inflate(this, R.layout.feeding, null);
		        SquirrelUIGenerator squirrel = new SquirrelUIGenerator(this);
		        squirrel.setTitle(R.string.feedTitle);
		        squirrel.setHelp(R.string.feedHelp);
		        squirrel.setBreadCrumb(R.string.feedBreadCrumb);
		        LinearLayout layout = new LinearLayout(this);
		        layout.setOrientation(LinearLayout.VERTICAL); //Set Oritentation
		        layout.addView(squirrel.getHeader());
		        layout.addView(inflatedView);
		        setContentView(layout);
		final  Button doneButton = (Button) findViewById(R.id.food_pred_done);
		        
		final EditText editText = (EditText)findViewById(R.id.feedingInputText);
		final SeekBar seekBar1 = (SeekBar) findViewById(R.id.food_seekBar1);
		final TextView seekBarValue1 = (TextView) findViewById(R.id.birdFeederText);
		final SeekBar seekBar2 = (SeekBar) findViewById(R.id.food_SeekBar01);
		final TextView seekBarValue2 = (TextView) findViewById(R.id.handOutsText);
		final SeekBar seekBar3 = (SeekBar) findViewById(R.id.food_SeekBar02);
		final TextView seekBarValue3 = (TextView) findViewById(R.id.garbageText);
		final SeekBar seekBar4 = (SeekBar) findViewById(R.id.food_SeekBar03);
		final TextView seekBarValue4 = (TextView) findViewById(R.id.treesText);
		final SeekBar seekBar5 = (SeekBar) findViewById(R.id.food_SeekBar04);
		final TextView seekBarValue5 = (TextView) findViewById(R.id.otherText);
		
		ArrayList<FeedType> data = SquirrelData.getFeedTypes();
		 editText.setText(SquirrelData.getAdditionalFeedData());
		setupSeekBar(seekBar1,seekBarValue1,data.get(0).getValue());
		setupSeekBar(seekBar2,seekBarValue2,data.get(1).getValue());
		setupSeekBar(seekBar3,seekBarValue3,data.get(2).getValue());
		setupSeekBar(seekBar4,seekBarValue4,data.get(3).getValue());
		setupSeekBar(seekBar5,seekBarValue5,data.get(4).getValue());
		
		
		doneButton.setOnClickListener(this);
		
		editText.addTextChangedListener(new TextWatcher(){

			public void afterTextChanged(Editable s) {
				SquirrelData.setFeederType("AdditionalComments",editText.getText().toString());
			}

			public void beforeTextChanged(CharSequence s, int start,
					int count, int after) {
				// TODO Auto-generated method stub
				
			}

			public void onTextChanged(CharSequence s, int start,
					int before, int count) {
				// TODO Auto-generated method stub
				
			}});
		seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub

				String temp = "";
				switch (progress) {
				case 0:
					temp = "Never";
					break;
				case 1:
					temp = "Seldom";
					break;
				case 2:
					temp = "Often";
					break;
				case 3:
					temp = "Regularly";
					break;
				default:
					temp = "Unknown.";
					break;
				}
				seekBarValue1.setText(temp);
				SquirrelData.setFeederType("Bird Feeders", temp.toUpperCase() );
			}

			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}

			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}
		});
		seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub

				String temp = "";
				switch (progress) {
				case 0:
					temp = "Never";
					break;
				case 1:
					temp = "Seldom";
					break;
				case 2:
					temp = "Often";
					break;
				case 3:
					temp = "Regularly";
					break;
				default:
					temp = "Unknown.";
					break;
				}
				seekBarValue2.setText(temp);
				SquirrelData.setFeederType("Handouts", temp.toUpperCase() );
			}

			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}

			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}
		});
		seekBar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub

				String temp = "";
				switch (progress) {
				case 0:
					temp = "Never";
					break;
				case 1:
					temp = "Seldom";
					break;
				case 2:
					temp = "Often";
					break;
				case 3:
					temp = "Regularly";
					break;
				default:
					temp = "Unknown.";
					break;
				}
				seekBarValue3.setText(temp);
				SquirrelData.setFeederType("Garbage", temp.toUpperCase() );
			}

			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}

			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}
		});
		seekBar4.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub

				String temp = "";
				switch (progress) {
				case 0:
					temp = "Never";
					break;
				case 1:
					temp = "Seldom";
					break;
				case 2:
					temp = "Often";
					break;
				case 3:
					temp = "Regularly";
					break;
				default:
					temp = "Unknown.";
					break;
				}
				seekBarValue4.setText(temp);
				SquirrelData.setFeederType("Trees", temp.toUpperCase() );
			}

			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}

			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}
		});
		seekBar5.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub

				String temp = "";
				switch (progress) {
				case 0:
					temp = "Never";
					break;
				case 1:
					temp = "Seldom";
					break;
				case 2:
					temp = "Often";
					break;
				case 3:
					temp = "Regularly";
					break;
				default:
					temp = "Unknown.";
					break;
				}
				seekBarValue5.setText(temp);
				SquirrelData.setFeederType("Other", temp.toUpperCase() );
			}

			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}

			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			}
		});


	}

	public void onClick(View v) {
		setResult(RESULT_OK);
		finish();
	}
		
	public void setupSeekBar(SeekBar bar,TextView text, String string){
		String temp = "";
		int value=0;
		if(string.equals("NEVER")){
			temp = "Never";
			value = 0;
		}
		else if(string.equals("SELDOM")){
			temp = "Seldom";
			value = 1;
		}
		else if(string.equals("OFTEN")){
			temp = "Often";
			value = 2;
		}
		else if(string.equals("REGULARLY")){
			temp = "Regularly";
			value = 3;
		}
		bar.setProgress(value);
		text.setText(temp);
		
	}
}
